
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require("xtpl");

const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));

// 教师列表

module.exports.teaList=(req,res)=>{

  
  const page=parseInt(req.query.page||'1');
  const limit=parseInt(req.query.limit||'12');
  const start=(page-1)*limit;

  db.table('teach').count("*").then((count)=>{

     const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
     const totalPageArray=[];
     for(var i=1;i<=totalPage;i++){
     	totalPageArray.push(i);
     }

     db.table('teach').order('teach_id DESC').limit(start,limit).select().then((data)=>{
          
        
    xtpl.renderFile(
    path.join(__dirname,"../views/admin/tea_list.html"),
    {

      array:data,
      totalPageArray:totalPageArray,
      page:page,
      totalPage:totalPage

    },
    (err,content)=>{
      if(err){
      	return console.log(err);
      }
      res.setHeader("Content-Type","text/html;charset=utf-8");
      res.end(content);
    }
  	);

         

     });
  });
 

};


module.exports.teaEdit=(req,res)=>{
const teach_id=req.params.teach_id;
const where=`teach_id=${teach_id}`;

db.table('teach').where(where).find().then((data)=>{

    if(JSON.stringify(data)=="{}"){
       
        base.shoeMsg(res,'教师不存在','/tea/tea_list');
        return false;

    }else{

   xtpl.renderFile(

   path.join(__dirname,"../views/admin/tea_edit.html"),
   {
   	teachInfo:data
   },
   (err,content)=>{
        
        if(err){

        	return console.log(err);
        }

       res.setHeader("Content-Type","text/html;charset=utf-8");
       res.end(content); 	

   }



	);

    }

});


};


module.exports.teaEditDate=(req,res)=>{

  const teach_id=req.params.teach_id||0;
  const where=`teach_id=${teach_id}`;

  db.table('teach').where(where).find().then((data)=>{
     
        if(JSON.stringify(data)=="{}"){

           base.showMsg(res,'教师不存在','/tea/tea_list');
           return false;
        }else{


       var check= `teach_name='${req.body.teach_name}' AND teach_id !=${teach_id} `;
        
        db.table('teach').where(check).find().then(function(data){
          
          if(JSON.stringify(data)!="{}"){
             
             base.showMsg(res,'教师已经存在');
             return false;
          }else{

        const data={
           "teach_name":req.body.teach_name,
           "teach_sex":req.body.teach_sex,
           "teach_tel":req.body.teach_tel,
           "teach_qq":req.body.teach_qq
        };

     db.table('teach').where(`teach_id=${teach_id}`).update(data).then(function(affectRows){

                   res.redirect('/tea/tea_list');
                   
                   return false;

              }).catch(function(err){

              if(err){
                    base.showMsg(res,"更新失败");
                    console.log(err);
                    return false;
                };
              });
          }

        });

        }   

  });

};